package com.interrobang.anime;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class FileImporter {

	private final String FILENAME = "Data/data.haruhi";
	private AnimeList list;
	private String title;
	private String episodes;
	private String rule;

	private boolean readingTitle;
	private boolean readingEpisode;
	private boolean readingRule;

	public FileImporter() 
	{
		title = "";
		episodes = "";
		rule = "";

		readingTitle = false;
		readingEpisode = false;
		readingRule = false;

		FileInputStream fis = null;
		ObjectInputStream in = null;
		try 
		{
			fis = new FileInputStream(FILENAME);
			in = new ObjectInputStream(fis);
			list = (AnimeList) in.readObject();
			in.close();
		} 
		catch (IOException e) 
		{
			list = new AnimeList();
		} 
		catch (ClassNotFoundException e) 
		{
			// do something here
		}

	}

	public void importFile(File fileName) throws FileNotFoundException 
	{
		Scanner scan = new Scanner(fileName);
		Anime anime = null;

		while (scan.hasNextLine()) 
		{
			Scanner line = new Scanner(scan.nextLine());

			while (line.hasNext()) 
			{
				String token = line.next();

				if (token.equals("*")) 
				{
					if (!rule.isEmpty()) 
					{
						// System.out.println(rule);
						Rule newRule = new Rule(rule);
						anime.addRule(newRule);
						rule = "";
					}

					title = "";

					readingTitle = true;
					readingEpisode = false;
					readingRule = false;
				} 
				else if (token.equals("|")) 
				{
					episodes = "";

					readingEpisode = true;
					readingTitle = false;
					readingRule = false;
				} 
				else if (token.equals("@")) 
				{
					if (!rule.isEmpty()) 
					{
						// System.out.println(rule);
						Rule newRule = new Rule(rule);
						anime.addRule(newRule);
					}

					rule = "";

					readingRule = true;
					readingTitle = false;
					readingEpisode = false;
				}

				if (readingTitle)
				{
					if (token.equals("*")) 
					{
						// do nothing
					} 
					else 
					{
						title += token + " ";
					}
				} 
				else if (readingEpisode) 
				{
					if (token.equals("|")) 
					{
						// do nothing
					} 
					else 
					{
						episodes += token;
						anime = new Anime(title, Integer.valueOf(episodes));
						list.add(anime);
						// System.out.print(title + " " + episodes + "\n");
					}
				} 
				else if (readingRule)
				{
					if (token.equals("@")) 
					{
						// do nothing
					} 
					else 
					{
						rule += token + " ";
					}
				}
			}
		}

		FileOutputStream fos = null;
		ObjectOutputStream out = null;
		try 
		{
			fos = new FileOutputStream(FILENAME);
			out = new ObjectOutputStream(fos);
			out.writeObject(list);
			out.close();
		} 
		catch (IOException ex) 
		{
			System.out.print("File not found");
		}
	}
}
